namespace Brjnk.GpxExplorer.Localization
{
    using System;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Markup;

    public class LocExtension : MarkupExtension
    {
        public LocExtension(string key)
        {
            Key = key;
        }

        public LocExtension()
        {
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Binding == null)
            {
                var b = LocalizationManager.Instance.GetBinding(Key);
                if (((IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget))).TargetObject.GetType() == typeof(Setter))
                {
                    return b;
                }
                else return b.ProvideValue(serviceProvider);
            }
            else
            {
                var b = new MultiBinding();
                b.Bindings.Add(LocalizationManager.Instance.GetBinding(Key));
                b.Bindings.Add(this.Binding);
                b.Converter = new LocalizationConverter();
                return b.ProvideValue(serviceProvider);
            }
        }

        [ConstructorArgument("key")]
        public string Key
        {
            get;
            set;
        }

        public System.Windows.Data.BindingBase Binding
        {
            get;
            set;
        }
    }
}